from django.template import loader, Context
from django.core.mail import EmailMessage
import django.dispatch
from django.db.models.signals import post_save

# Define signal for sending email
send_mail = django.dispatch.Signal(providing_args = ["email", "model", "subject", "template"])

# define send email method
def send_email(sender, **kwargs):

    # handle the send to user
    recipients = kwargs['email']
    model = kwargs['model']
    subject = kwargs['subject']
    template = kwargs['template']
    context = Context({'recipients': recipients, 'model': model})
    t = loader.get_template(template)
    html = t.render(context)
    msg = EmailMessage(subject, html,
                        'anupam.jindal1@gmail.com', recipients)
    msg.content_subtype = "html"
    msg.send()

# catch the signal
send_mail.connect(send_email)


# Email confniguration for single recipient emails
# Define signal for sending email
send_single_mail = django.dispatch.Signal(providing_args = ["email", "name", "model", "subject", "template"])

# define send email method
def send_single_email(sender, **kwargs):

    # handle the send to user
    recipients = kwargs['email']
    model = kwargs['model']
    name = kwargs['name']
    subject = kwargs['subject']
    template = kwargs['template']
    context = Context({'recipients': recipients, 'model': model, 'name': name})
    t = loader.get_template(template)
    html = t.render(context)
    msg = EmailMessage(subject, html,
                        'anupam.jindal1@gmail.com', recipients)
    msg.content_subtype = "html"
    msg.send()

# catch the signal
send_single_mail.connect(send_single_email)